﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls;
using System.Xml;
using JQueryControlToolkit;
using UIT.Animations;
using UIT.Web.Renderers.Animations;

namespace UIT.Web.Renderers.Specials
{
    public static class VisualStateRenderer
    {
        public static void RenderVisualStates(this IControl control, XmlWriter writer)
        {
            foreach (VisualStates.VisualState state in control.VisualStates)
            {
                string eventName = GetClientSideEventName(state.Name);
                JSFunction changeState = new JSFunction();
                Dictionary<string, object> css = new Dictionary<string, object>();
                if (state.Style != null)
                {
                    string style = state.Style.Render();
                    string[] cssOptions = style.Split(';', ':');
                    for (int i = 0; i < cssOptions.Length; i += 2)
                        css.Add(cssOptions[i], cssOptions[i + 1]);

                    changeState.Body.Add(new JQuery("#" + control.Id).Css(css));
                }
                if (state.Enter != null)
                {
                    changeState = new JSFunction();
                    foreach (IAnimation animation in state.Enter.Animations)
                    {
                        changeState.Body.Add(animation.Render());
                    }
                    if (css.Count > 0)
                        changeState.Body.Add(new JQuery("#" + control.Id).Delay(0).Css(css));
                }
                ScriptManager.RegisterCall("visualState_Enter"+state.GetHashCode(),new JQuery("#" + control.Id).Live(eventName, changeState));

                if (state.Exit != null)
                {
                    changeState = new JSFunction();
                    foreach (IAnimation animation in state.Exit.Animations)
                    {
                        changeState.Body.Add(animation.Render());
                    }
                    ScriptManager.RegisterCall("visualState_Exit"+state.GetHashCode(), new JQuery("#" + control.Id).Live(GetClientSideEventName("!" + state.Name), changeState));
                }

            }
        }

        private static string GetClientSideEventName(string serverEventName)
        {
            switch (serverEventName)
            {
                case "Mouse.DoubleClick":
                    return "dblclick";
                case "Mouse.Click":
                    return "click";
                case "Hover":
                case "Mouse.Enter":
                    return "mouseenter";
                case "!Hover":
                case "Mouse.Leaves":
                    return "mouseleave";
                case "Mouse.Move":
                    return "mousemove";
                case "Keyboard.KeyDown":
                    return "keydown";
                case "Keyboard.KeyPress":
                    return "keypress";
                case "Keyboard.KeyUp":
                    return "keyup";
                default:
                    return serverEventName;
            }
        }

        public static JSObject Render(this IAnimation animation)
        {
            if (animation.Target == null)
                throw new InvalidOperationException("The target for the animation was not provided");


            IWebAnimationRenderer renderer = (IWebAnimationRenderer)UIT.Context.Current.RendererManager.Animation.GetRenderer(animation);
            renderer.Render(animation);

            var animate = new JQuery("#" + animation.Target.Id).Animate(new Dictionary<string, object> { { StyleRenderer.GetCssProperty(animation.TargetStyleProperty), renderer.TargetValue } }, (int)animation.Duration.TotalMilliseconds);

            return animate;
        }
    }
}
